package com.weimob.mcloud.wechatwork.customer.third;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.common.enums.CommonBizErrorEnum;
import com.weimob.mcloud.wechatwork.common.enums.MainBizDomainEnum;
import com.weimob.mcloud.wechatwork.common.enums.SubBizDomainEnum;
import com.weimob.mcloud.wechatwork.common.utils.BizErrorCodeGenerator;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/3 11:04 上午
 */
@Component
@Slf4j
public class PrivateSphereThird {

    @Reference
    private PrivateSphereService privateSphereService;

    /**
     * 企微助手产品id
     */
    @Value("${aewc.productId:203}")
    private Long aewcProductId;

    public PrivateSphere fetch(String corpId) {
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);
        privateSphere.setProductId(aewcProductId);

        SoaResponse<PrivateSphere, Void> privateSphereVoidSoaResponse = privateSphereService.fetch(privateSphere);
        return SoaUtil.unpack(privateSphereVoidSoaResponse);
    }

    public PrivateSphere fetch(String corpId, String templateId) {
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);
        privateSphere.setTemplateId(templateId);
        privateSphere.setProductId(aewcProductId);

        SoaResponse<PrivateSphere, Void> privateSphereVoidSoaResponse = privateSphereService.fetch(privateSphere);
        return SoaUtil.unpack(privateSphereVoidSoaResponse);
    }

    public PrivateSphere fetch(String corpId, Long bosId, String privateSphereId, Long productInstanceId) {
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);
        privateSphere.setBosId(bosId);
        privateSphere.setPrivateSphereId(privateSphereId);
        privateSphere.setProductInstanceId(productInstanceId);
        privateSphere.setProductId(aewcProductId);

        SoaResponse<PrivateSphere, Void> privateSphereVoidSoaResponse = privateSphereService.fetch(privateSphere);
        return SoaUtil.unpack(privateSphereVoidSoaResponse);
    }

    public PrivateSphere fetchByBosId(Long bosId) {
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setBosId(bosId);
        privateSphere.setProductId(aewcProductId);

        SoaResponse<PrivateSphere, Void> privateSphereVoidSoaResponse = privateSphereService.fetch(privateSphere);
        return SoaUtil.unpack(privateSphereVoidSoaResponse);
    }

    /**
     * JNBY多bos兼容，需要通过bosId pid piid确定唯一私域
     * @param bosId
     * @param basicInfo
     * @return
     */
    public PrivateSphere fetchByBosIdCorpId(com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo basicInfo) {
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setBosId(basicInfo.getBosId());
        privateSphere.setProductId(basicInfo.getProductId());
        privateSphere.setProductInstanceId(basicInfo.getProductInstanceId());
        privateSphere.setProductId(aewcProductId);

        SoaResponse<PrivateSphere, Void> privateSphereVoidSoaResponse = privateSphereService.fetch(privateSphere);
        return SoaUtil.unpack(privateSphereVoidSoaResponse);
    }

    /**
     * 私域simpleFetch
     *
     * @param basicInfo basicInfo
     * @param simpleFetch true表示只查询私域表信息，false表示查全部信息
     * @return PrivateSphere
     */
    public PrivateSphere fetch(BasicInfo basicInfo, Boolean simpleFetch) {
        //入参组装
        Long bosId = basicInfo.getBosId();
        Long productId = basicInfo.getProductId();
        if (null == productId) {
            productId = aewcProductId;
        }
        Long productInstanceId = basicInfo.getProductInstanceId();
        String corpId = basicInfo.getCorpId();
        log.info("convertPrivateSphere bosId:{},corpid:{},productId:{},productInstanceId:{}", bosId, corpId, productId, productInstanceId);
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setBosId(bosId);
        privateSphere.setCorpId(corpId);
        privateSphere.setProductId(productId);
        privateSphere.setProductInstanceId(productInstanceId);
        //查询私域id
        PrivateSphere privateSphereResponse;
        if (Boolean.TRUE.equals(simpleFetch)) {
            privateSphereResponse = SoaUtil.unpack(privateSphereService.simpleFetch(privateSphere));
        } else {
            privateSphereResponse = SoaUtil.unpack(privateSphereService.fetch(privateSphere));
        }
        if (Objects.isNull(privateSphereResponse)) {
            String errorCode = BizErrorCodeGenerator.generate(
                    MainBizDomainEnum.CUSTOMER_MANAGEMENT, SubBizDomainEnum.PRIVATE_SPHERE_RELATED,
                    CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE);
            log.error("privateSphereResponse is null");
            throw new BizException(Long.parseLong(errorCode), CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE.getDesc());
        }
        String privateSphereId = privateSphereResponse.getPrivateSphereId();
        if (StringUtils.isEmpty(privateSphereId)) {
            throw new BizException("未获取到私域privateSphereId");
        }
        return privateSphereResponse;
    }


}
